package com.chenhs.oop.study.反射.实例2;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

/**
 * <ul>peanut 学习
 * <li> Author:      chenhs</li>
 * <li>E-Mail:    </li>
 * <li> Date:     2021/6/6 - 21:27</li>
 * <li>Description:        注释  </li>
 * <li>+-History-------------------------------------+</li>
 * <li>| Date          Author       Description    </li>
 * <li>|2021/6/6 - 21:27         chenhs       Created   </li>
 * <li>+------------------------------------------------</li>
 * </ul>
 */
public class ApplicationContext<T> {
	//存放实例的容器
	private static HashMap<Class, Object> beanFactory = new HashMap<>();
	
	//获取实例的方法
	T getBean(Class clazz) {
		return (T) beanFactory.get(clazz);
	}
	
	//	系统初始化时创建所有实例
	 static void initContext() throws IOException, ClassNotFoundException {
		InputStream resource = ApplicationContext.class.getClassLoader().getResourceAsStream("bean.config");
		Properties properties = new Properties();
		properties.load(resource);
		//获取所有的key
		Set<Object> keys = properties.keySet();
		for (Object key : keys) {
			beanFactory.put(
					//通过全类路径创建获取一个类
					Class.forName(key.toString()), Class.forName(properties.get(key).toString()));
		}
	}
}
